package com.leetcode.LeetCode精选TOP面试题;

import com.leetcode.common.model.TreeNode;

/**
 * @author 覃国强
 * @date 2022-06-16 15:52
 */
public class 题库_0108_将有序数组转换为二叉搜索树 {

  static
  class Solution {
    public TreeNode sortedArrayToBST(int[] nums) {
      if (nums == null || nums.length == 0) {
        return null;
      }
      return helper(nums, 0, nums.length - 1);
    }

    private TreeNode helper(int[] nums, int l, int r) {
      if (l > r) {
        return null;
      }
      int mid = (l + r) / 2;
      TreeNode root = new TreeNode(nums[mid]);
      root.left = helper(nums, l, mid - 1);
      root.right = helper(nums, mid + 1, r);
      return root;
    }
  }

}
